package com.atguigu;

import io.jsonwebtoken.Jwt;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.SimpleFormatter;

public class JWTjiami {

    @Test
    public void Testst(){
       //私人信息
       Map<String, Object> map = new HashMap<>();
        //先进行JWT加密
       //服务器密钥
      String tokenKey = "atguigu";
      //添加盐值可以防止token被盗
         String ip="127.0.0.1";

        //生成 TOKEN的三部分 HEADER 荷载信息 签名
        map.put("username","龙哥");
        map.put("nickname","小龙人");
        map.put("爱好","playGame");

        Date date = new Date();
        //格式化操作
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH-mm-ss");
        String time = format.format(date);
        System.out.println(time);

        String token =  Jwts.builder().setHeaderParam("typ","JWT")
                .setHeaderParam("alg","HS256")
                 //头，荷载，加密类型
                .setClaims(map).signWith(SignatureAlgorithm.HS256,tokenKey+ip).compact();
        System.out.println(token);

        /**
        * Description: 签名只和荷载和加密类型盐值有关系
        * date: 2021/8/5 11:30
        * @author XIAOLONGSANG
        */
        String FIRST="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyLniLHlpb0iOiJwbGF5R2FtZSIsIm5pY2tuYW1lIjoi5bCP6b6Z5Lq6IiwidXNlcm5hbWUiOiLpvpnlk6UifQ.hO8aMAQN7oYEov7Ml1MYbjbzwO8tWfvQF9w7J_8hAJw";
        Jwt parse = Jwts.parser().setSigningKey(tokenKey + ip).parse(FIRST);
        System.out.println(parse);
        


    }

}
